package com.vcc.boss.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vcc.core.annotations.RepeatSubmit;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.ClientParam;
import com.vcc.data.entity.ClientUserParam;
import com.vcc.service.boss.request.clientParam.BossClientParamCreateRequest;
import com.vcc.service.boss.request.clientParam.BossClientUserParamSetRequest;
import com.vcc.service.boss.request.clientParam.BossClientParamUpdateRequest;
import com.vcc.service.boss.response.clientParam.ClientParamVoBoss;
import com.vcc.service.boss.response.clientParam.ClientUserParamVoBoss;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.ClientParamService;
import com.vcc.service.service.ClientUserParamService;
import com.vcc.service.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/client/param")
public class ClientParamController {


    @Resource
    private ClientParamService clientParamService;
    @Resource
    private ClientUserParamService clientUserParamService;
    @Resource
    private UserService userService;


    @RestPack
    @GetMapping("/list")
    @SaCheckLogin
    @SaCheckPermission(value = "client:param:list:view")
    public List<ClientParamVoBoss> list(String code, String name) {
        return clientParamService.list(code, name,null);
    }

    @RestPack
    @PostMapping("/create")
    @SaCheckPermission(value = "client:param:create")
    @RepeatSubmit
    public boolean create(@RequestBody @Validated BossClientParamCreateRequest bossClientParamCreateRequest) {
        if (clientParamService.existsByCode(bossClientParamCreateRequest.getCode())) {
            throw new IllegalArgumentException("Code already exists");
        }
        ClientParam clientParam = new ClientParam();
        clientParam.setName(bossClientParamCreateRequest.getName());
        clientParam.setCode(bossClientParamCreateRequest.getCode());
        clientParam.setValue(bossClientParamCreateRequest.getValue());
        clientParam.setHasUserConfig(bossClientParamCreateRequest.getHasUserConfig());
        return clientParamService.save(clientParam);
    }

    @RestPack
    @PostMapping("/update")
    @SaCheckPermission(value = "client:param:edit")
    @SaCheckLogin
    @RepeatSubmit
    public boolean update(@RequestBody BossClientParamUpdateRequest bossClientParamUpdateRequest) {
        return clientParamService.updateClientParam(bossClientParamUpdateRequest);
    }

    @RestPack
    @GetMapping("/listByUserId")
    @SaCheckPermission(value = "client:param:user:list:view")
    @SaCheckLogin
    public List<ClientUserParamVoBoss> listByUserId(Long targetUserId, String code, String name) {
        Long dealerId = userService.getDealerIdById(targetUserId);
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())){
            throw new ProgramException("无权查看该用户的参数配置");
        }
        List<ClientParamVoBoss> list = clientParamService.list(code, name,null);
        List<ClientUserParam> clientUserParams = clientUserParamService.listByUserId(targetUserId);
        return list.stream().map(it -> {
            ClientUserParam clientUserParam = clientUserParams.stream().filter(temp -> temp.getClientParamId().equals(it.getId()))
                    .findFirst().orElse(null);
            ClientUserParamVoBoss clientUserParamVo = new ClientUserParamVoBoss();
            clientUserParamVo.setId(clientUserParam != null ? clientUserParam.getId() : null);
            clientUserParamVo.setValue(clientUserParam != null ? clientUserParam.getValue() : null);
            clientUserParamVo.setClientParamId(it.getId());
            clientUserParamVo.setName(it.getName());
            clientUserParamVo.setCode(it.getCode());
            clientUserParamVo.setDefaultValue(it.getValue());
            clientUserParamVo.setHasUserConfig(it.getHasUserConfig());
            return clientUserParamVo;
        }).toList();
    }


    @RestPack
    @PostMapping("setUserParam")
    @SaCheckPermission(value = "client:param:user:edit")
    @SaCheckLogin
    @RepeatSubmit
    public Boolean setUserParam(@RequestBody @Validated BossClientUserParamSetRequest setClientUserParamDto) {
        Long dealerId = userService.getDealerIdById(setClientUserParamDto.getTargetUserId());
        if (!BossUserUtil.hasAllData() && !dealerId.equals(BossUserUtil.getId())){
            throw new ProgramException("无权查看该用户的参数配置");
        }
        ClientParam clientParam = clientParamService.getByCode(setClientUserParamDto.getCode());
        if (!clientParam.getHasUserConfig()){
            throw new ProgramException("该参数不允许用户单独配置");
        }
        try {
            ClientUserParam clientUserParam = clientUserParamService.getByUserIdAndParamId(setClientUserParamDto.getTargetUserId(), clientParam.getId());
            if (!setClientUserParamDto.getValue().equals(clientUserParam.getValue())) {
                clientUserParamService.updateValue(clientUserParam.getId(), setClientUserParamDto.getValue());
            }
        } catch (ProgramException e) {
            ClientUserParam clientUserParam = new ClientUserParam();
            clientUserParam.setUserId(setClientUserParamDto.getTargetUserId());
            clientUserParam.setClientParamId(clientParam.getId());
            clientUserParam.setValue(setClientUserParamDto.getValue());
            clientUserParamService.save(clientUserParam);
        }
        return true;
    }
}
